package com.snapp.web.agents;

import java.util.HashMap;
import java.util.Vector;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;

import com.snapp.ejb.interfaces.remote.SNAppManagerRemote;
import com.snapp.util.object.model.ObjectModel;

public class SNAppAgent {
	private static Logger logger = Logger.getLogger(SNAppAgent.class);
	
	// default ejb context..
	private String ejbContext = "snapp/SNAppManagerBean/remote";
	// EJB Manager bean
	private Object manager = null;
	
	/**
	 * EJB lookup method for agents

	 * @param name
	 * @return
	 */
	public static Object lookupBean (String name) {
		Object o = null;
		logger.info("~~ Looking up EJB: " + name);
		
		try {
			InitialContext ctx = new InitialContext();
			o = ctx.lookup(name);
		}
		catch(NamingException nex) {
			logger.error("Error Fetching Bean Ref (" + name + ")" ,nex);
			o = null;
		}
		return o;
	}


	public String getEjbContext() {
		return ejbContext;
	}


	public void setEjbContext(String ejbContext) {
		this.ejbContext = ejbContext;
	}


	public Object getManager() throws Exception {
		if (manager == null)
			manager = lookupBean(ejbContext);
		
		return manager;
	}


	public void setManager(Object manager) {
		this.manager = manager;
	}

	/******************************************************************************************************************************************/
	
	/**
	 * 
	 * @param snappGlobalId
	 * @param objId
	 * @return
	 */
	public ObjectModel load(String snappGlobalId, HashMap<String, String> params) throws Exception{
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.load(snappGlobalId, params);
	}
	
	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @return
	 */
	public ObjectModel save(String snappGlobalId, HashMap<String,Object> params) throws Exception {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.save(snappGlobalId, params);
	}
	
	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @return
	 */
	public ObjectModel saveList(String snappGlobalId, HashMap<String,Object> params, String delimiter) throws Exception {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.saveList(snappGlobalId, params, delimiter);
	}

	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @param orderBy
	 * @return
	 */
	public Vector<ObjectModel> list(String snappGlobalId, HashMap<String,String> params) throws Exception  {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.list(snappGlobalId, params);
	}
	
	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @param orderBy
	 * @return
	 */
	public int count(String snappGlobalId, HashMap<String,String> params) throws Exception  {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.count(snappGlobalId, params);
	}

	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @return
	 */
	public void delete(String snappGlobalId, HashMap<String,String> params) throws Exception {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		dmb.delete(snappGlobalId, params);
	}

	/**
	 * 
	 * @param snappGlobalId
	 * @param objId
	 * @return
	 */
	public ObjectModel load(String snappGlobalId, String params) throws Exception  {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.load(snappGlobalId, params);
	}
	
	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @param orderBy
	 * @return
	 */
	public Vector<ObjectModel> list(String snappGlobalId, String params) throws Exception  {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.list(snappGlobalId, params);
	}
	
	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @param orderBy
	 * @return
	 */
	public int count(String snappGlobalId, String params) throws Exception  {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		return dmb.count(snappGlobalId, params);
	
	}

	/**
	 * 
	 * @param snappGlobalId
	 * @param params
	 * @return
	 */
	public void delete(String snappGlobalId, String params) throws Exception {
		SNAppManagerRemote dmb = null;
		dmb = (SNAppManagerRemote)getManager();
		dmb.delete(snappGlobalId, params);
	}

}
